﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using COMOSOFT.Class;
using COMOSOFT.Main;
using DevExpress.XtraGrid.Columns;

namespace COMOSOFT.OrderBusiness.Forms
{
    public partial class TabGeneralView : Form
    {
        ClassShare cls = new ClassShare("TabGeneralView");
        Task tak = null;
        FrmMain frmain = null;
        int flag = 0;     
        int year;
        object month,cid;
        public TabGeneralView(FrmMain frm)
        {
            InitializeComponent();
            this.frmain = frm;
        }

        #region [Refresh data]
        public void RefreshData()
        {
            try
            {
                sp_GeneralViewTableAdapter.Fill(dataSetOrderManagement.sp_GeneralView, ClassShare.Usid,0,year,month,cid);
                //orderSizeBreakdownTableAdapter.Fill(dataSetOrderManagement.OrderSizeBreakdown);                
                sp_CustomerListTableAdapter.Fill(dataSetOrderManagement.sp_CustomerList, ClassShare.Usid);
                sp_OrderStyleListTableAdapter.Fill(dataSetOrderManagement.sp_OrderStyleList, ClassShare.Usid);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region[Load GeneralView]
        public void LoadGV(int y, object m, object cid)
        {
            this.year = y;
            this.month = m;            
            this.cid = cid;
            flag = 0;
            radioGroup_Menu.SelectedIndex = 0;
            radioGroup_Menu.Properties.Items[1].Description = "On Going " + this.year.ToString();
            radioGroup_Menu.Properties.Items[2].Description = "All Order " + this.year.ToString();
            CallGeneral(y, m, cid);
        }      
        public void CallGeneral(int y, object m, object cid)
        {
            try
            {
                if (tak == null || tak.Status != TaskStatus.Running)
                {
                    progressPanel1.Show();
                    progressPanel1.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2 - progressPanel1.Width, Screen.PrimaryScreen.WorkingArea.Height / 2 - progressPanel1.Height);
                    gridControlGaneral.DataSource = null;
                    tak = Task.Factory.StartNew(() => LoadGaneral(y, m, cid));
                    tak.ContinueWith(tt => FillTable());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private delegate void dlgFillTable();
        private void FillTable()
        {
            try
            {
                if (InvokeRequired)
                {
                    this.Invoke(new dlgFillTable(FillTable));
                }
                else
                {
                    gridControlGaneral.DataSource = dataSetOrderManagement.sp_GeneralView;
                    progressPanel1.Hide();
                }
            }
            catch 
            {
                MessageBox.Show("Progess fail");
            }
        }
        private void LoadGaneral(int y, object m, object cid)
        {
            try
            {
                sp_GeneralViewTableAdapter.Fill(dataSetOrderManagement.sp_GeneralView, ClassShare.Usid, flag, y, m, cid);
                orderSizeBreakdownTableAdapter.Fill(dataSetOrderManagement.OrderSizeBreakdown);
                sp_CustomerListTableAdapter.Fill(dataSetOrderManagement.sp_CustomerList, ClassShare.Usid);
                sp_OrderStyleListTableAdapter.Fill(dataSetOrderManagement.sp_OrderStyleList, ClassShare.Usid);
            }
            catch
            {
                MessageBox.Show("Disconnection database! You can try later");
            }
        }
        #endregion

        #region [Update Orderdetails]
        private void gridView_Order_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            if (dataSetOrderManagement.sp_GeneralView.Rows.Count > 0)
            {
                gridView_Order.SetRowCellValue(e.RowHandle, "EID", dataSetOrderManagement.sp_GeneralView.Rows[dataSetOrderManagement.sp_GeneralView.Rows.Count - 1]["EID"]);
            }
        }
        private void gridView_Order_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            try
            {
                spGeneralViewBindingSource.EndEdit();
                sp_GeneralViewTableAdapter.Update(dataSetOrderManagement.sp_GeneralView);
                dataSetOrderManagement.sp_GeneralView.AcceptChanges();
            }
            catch 
            {
                MessageBox.Show("Upadate Failed!");
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView_Order.FocusedRowHandle > 0)
                {
                    if (XtraMessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        spGeneralViewBindingSource.EndEdit();
                        gridView_Order.DeleteSelectedRows();
                        sp_GeneralViewTableAdapter.Update(dataSetOrderManagement.sp_GeneralView);
                        dataSetOrderManagement.sp_GeneralView.AcceptChanges();
                    }
                }
                else
                {
                    MessageBox.Show("Not choose Order!");
                }
            }
            catch
            {
                MessageBox.Show("Can't delete object");
            }
        }
        #endregion

        #region [Filter OrderStyleList]
        private void repositoryItemGridLookUpEdit_StyleList_Click(object sender, EventArgs e)
        {
            GridLookUpEdit glk = sender as GridLookUpEdit;
            GridView view = glk.Properties.View as GridView;
            view.ActiveFilterString = "[Customer] = '" + gridView_Order.GetFocusedRowCellValue("Customer").ToString() + "'";           
        }
        #endregion

        #region[Update OrderStyleList]
        private void reposStyleListView_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            GridView view = sender as GridView;
            if (dataSetOrderManagement.sp_OrderStyleList.Rows.Count > 0)
            {
                view.SetRowCellValue(e.RowHandle, "Customer", gridView_Order.GetFocusedDataRow()["Customer"]);
                view.SetRowCellValue(e.RowHandle, "Creater", gridView_Order.GetFocusedDataRow()["EID"]);
            }
        }
        private void reposStyleListView_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            try
            {
                spOrderStyleListBindingSource.EndEdit();
                sp_OrderStyleListTableAdapter.Update(dataSetOrderManagement.sp_OrderStyleList);
                dataSetOrderManagement.sp_OrderStyleList.AcceptChanges();
            }
            catch
            {
                MessageBox.Show("Please try again!!","Not Insert...",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void deleteToolStripMenuItem_delete_Click(object sender, EventArgs e)
        {
            try
            {
                if (XtraMessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string styleno=gridView_Order.GetFocusedDataRow()["StyleNo"].ToString();
                    spOrderStyleListBindingSource.EndEdit();
                    sp_OrderStyleListTableAdapter.Delete(styleno);
                    sp_OrderStyleListTableAdapter.Update(dataSetOrderManagement.sp_OrderStyleList);
                    dataSetOrderManagement.sp_OrderStyleList.AcceptChanges();
                }
            }
            catch
            {
                MessageBox.Show("Can't delete object");
            }
        }
        #endregion

        #region [RepossStyleListView_DoubleClick]
        private void reposStyleListView_DoubleClick(object sender, EventArgs e)
        {
            reposStyleListView.Columns["Customer"].Visible = true;
            reposStyleListView.Columns["Description"].Visible = true;
            reposStyleListView.Columns["ItemGroup"].Visible = true;
            reposStyleListView.Columns["Creater"].Visible = true;
            reposStyleListView.Columns["Remarks"].Visible = true;
        }
        #endregion

        private void TabGeneralView_Load(object sender, EventArgs e)
        {          
            this.employeeTableAdapter.Fill(this.dataSetOrderManagement.Employee);           
        }

        #region [Order Status]
        private void radioGroup_Menu_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (radioGroup_Menu.SelectedIndex == 0)
                {
                    flag = 0;
                }
                if (radioGroup_Menu.SelectedIndex == 1)
                {
                    flag = 1;
                }
                if (radioGroup_Menu.SelectedIndex == 2)
                {
                    flag = 2;
                }
                if (radioGroup_Menu.SelectedIndex == 3)
                {
                    flag = 3;
                }
                this.month = null;
                this.cid = null;
                CallGeneral(year, month, cid);
            }
            catch
            {
                MessageBox.Show("Not access database!");
            }
        }
        #endregion

        private void sizeBreakdownToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int OID = Convert.ToInt32(gridView_Order.GetFocusedDataRow()["OID"]);
            FrmSizeBreakdown frSBD = new FrmSizeBreakdown();
            frSBD.OIDs.Add(OID);
            frSBD.Show();
        }

        #region [load Factory Purchase Master]
        private void factoryPurchaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                FrmFactoryPurchaseMaster frFPM = new FrmFactoryPurchaseMaster(-1) { MdiParent = this.frmain, Text = "PO: Add new Order" };
                int[] numrow = gridView_Order.GetSelectedRows();
                foreach (int i in numrow)
                {
                    DataRow row = gridView_Order.GetDataRow(i);
                    if (i == 0)
                        frFPM.OIDS = row["OID"].ToString();
                    else
                        frFPM.OIDS += "," + row["OID"].ToString();
                }
                frFPM.Show();
                frFPM.LoadFPM();
                frFPM.NewFPM();                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        #endregion
    }
}
